<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;


class MyController extends Controller
{
   public  function beforeAction($action) {
       #parent::beforeAction($action);
       $actionID = Yii::$app->controller->action->id;//当前方法

        //其他页面没有登陆回到登陆页面
        if (!$this->isLogin() && (!in_array($actionID, array('login','captcha')))) {
           $this->redirect(['site/login']);
        }
        //如果登陆过了，跳转到登陆页面自动跳转到首页
        if ($this->isLogin() && ($actionID =='login')) {
            $this->redirect(['site/index']);
        }

        return  parent::beforeAction($action);
   }
   
   private   function isLogin() {
       return  md5((Yii::$app->session['admin_id'].Yii::$app->request->hostInfo)) == Yii::$app->session['admin_idtoken'] ? 1 : 0;
   }
   
}
